package ikudyk.university;

/* Storage for operators / expressions io file handling*/

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Utils {
    public static Operator[] operators;
    public static String[] expressions;

    public static String[] getExpressions() {
        return expressions;
    }
    public static Operator[] getOperators() {
        return operators;
    }

    public static void initOperators() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("OPERATORS.TXT"));
        int operatorsQuantity = Integer.valueOf(bufferedReader.readLine());
        if (operatorsQuantity <= 0) {
            throw new IOException("Negative operators quantity");
        }
        operators = new Operator[operatorsQuantity];
        String[] operatorParts;
        for (int i = 0; i < operatorsQuantity; i++) {
            operatorParts = bufferedReader.readLine().split(" ");
            try {
                operators[i] = new Operator(operatorParts[0].charAt(0),
                        Integer.valueOf(operatorParts[1]),
                        Integer.valueOf(operatorParts[2]),
                        Integer.valueOf(operatorParts[3]),
                        Boolean.valueOf(operatorParts[4]));
            } catch (Exception e) {
                throw new IOException("Input file has a mistake (line " + i + ")");
            }
        }
    }

    public static void initInput() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("INPUT.TXT"));
        int linesQuantity = Integer.valueOf(bufferedReader.readLine());
        if(linesQuantity <= 0) {
            throw new IOException("Negative expression quantity");
        }
        expressions = new String[linesQuantity];
        for(int i = 0; i < linesQuantity; i++) {
            expressions[i] = bufferedReader.readLine();
        }
    }
}
